<?php


namespace apps\marriage\app\mp\controller;

use apps\marriage\core\model\MemberRechargeRecord;
use apps\marriage\core\model\UserChatMessage;
use apps\marriage\core\model\UserSystemNotice;
use apps\marriage\core\service\MemberProfileService;
use apps\marriage\core\service\MemberRechargeRecordService;
use apps\marriage\core\service\UserChatMessageService;
use apps\marriage\core\service\UserSystemNoticeService;
use think\Request;

class UserChatMessageController extends BaseMarriageMpController
{
    /**
     * @var UserChatMessageService
     */
    private $userChatMessageService;

    /**
     * @var MemberRechargeRecordService
     */
    private $memberRechargeRecordService;

    /**
     * @var UserSystemNoticeService
     */
    private $userSystemNoticeService;

    /**
     * @var MemberProfileService
     */
    private $memberProfileService;

    protected function init()
    {
        parent::init();
        $this->userSystemNoticeService = service('UserSystemNotice', SERVICE_NAMESPACE);
        $this->userChatMessageService = service('UserChatMessage', SERVICE_NAMESPACE);
        $this->memberRechargeRecordService = service('MemberRechargeRecord', SERVICE_NAMESPACE);
        $this->memberProfileService = service('MemberProfile', SERVICE_NAMESPACE);
    }

    public function send(Request $request)
    {
        $toUid = $request->param('to_uid', 0, 'intval');
        $msg = $request->param('msg', '');
        $chat = $this->userChatMessageService->send($this->user['id'], $toUid, $msg);
        return $this->success([
            'chat' => $chat
        ]);
    }

    public function send_auth_notice(Request $request)
    {
        $toUid = $request->param('to_uid', 0, 'intval');
        $msg = '请完成实名认证，期待与您相识、相知。';
        $this->userChatMessageService->send($this->user['id'], $toUid, $msg, false);
        return $this->success();
    }

    public function sessions(Request $request)
    {
        $sessions = $this->userChatMessageService->getMyChatSessions($this->user['id']);
        return $this->success([
            'sessions' => $sessions
        ]);
    }

    public function session(Request $request)
    {
        $sendUid = $request->param('send_uid', 0, 'intval');
        $msgs = $this->userChatMessageService->getSession($this->user['id'], $sendUid);
        $receiverMemberRights = $this->memberProfileService->getVipRightsByUserId($this->user['id']);
        $senderMemberRights = $this->memberProfileService->getVipRightsByUserId($sendUid);
        $unlock = false;
        if($senderMemberRights['send_msg_auto_unlock_for_receiver_enable']
            || $receiverMemberRights['receive_msg_auto_unlock_enable']) {   // 收信人有权免费查看，或发信人自动帮对方解锁
            $unlock = true;
        } else {
            if($this->memberRechargeRecordService->info([
                'user_id' => $this->user['id'],
                'iid' => $sendUid,
                'iid_type' => MemberRechargeRecord::IID_TYPE_UNLOCK_CHAT_SESSION
            ])) {
                $unlock = true;
            }
        }
        return $this->success(['msgs' => $msgs, 'unlock' => $unlock]);
    }

    public function read(Request $request)
    {
        $id = $request->param('id', 0, 'intval');
        $this->userChatMessageService->updateByPk([
            'id' => $id,
            'status' => UserChatMessage::STATUS_READ
        ]);
        return $this->success();
    }

    public function new_amount()
    {
        $amount = $this->userChatMessageService->count([
            'to_uid' => $this->user['id'],
            'status' => UserChatMessage::STATUS_WAIT_READ
        ]);

        $amount2 = $this->userSystemNoticeService->count([
            'recipient_id' => $this->user['id'],
            'status' => UserSystemNotice::STATUS_WAIT_READ
        ]);

        return $this->success([
            'chat_message' => $amount,
            'system_notice' => $amount2
        ]);
    }
}